package com.ruoyi.insect.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.insect.domain.DiseaseIdentify;
import com.ruoyi.insect.mapper.DiseaseIdentifyMapper;
import com.ruoyi.insect.service.IDiseaseIdentifyService;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 病虫害识别Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-07
 */
@Service
public class DiseaseIdentifyServiceImpl implements IDiseaseIdentifyService
{
    @Value("${ai.service.url}")
    private String aiServiceUrl;

    private final DiseaseIdentifyMapper pestRecognitionRecordMapper;

    public DiseaseIdentifyServiceImpl(DiseaseIdentifyMapper pestRecognitionRecordMapper) {
        this.pestRecognitionRecordMapper = pestRecognitionRecordMapper;
    }

    @Override
    public Map<String, Object> recognizePest(MultipartFile file) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(aiServiceUrl + "/predict");

        // 构建多部分请求
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("file", file.getInputStream(), ContentType.APPLICATION_OCTET_STREAM, file.getOriginalFilename());
        HttpEntity multipart = builder.build();
        httpPost.setEntity(multipart);

        // 发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);

        //获取响应内容
        byte[] imageData=EntityUtils.toByteArray(response.getEntity());

        //将图片数据转换为Base64字符串
        String base64Image=java.util.Base64.getEncoder().encodeToString(imageData);

        Map<String,Object> result=new HashMap<>();
        result.put("image",base64Image);
        result.put("success",true);

        httpClient.close();
        return result;
    }

    /**
     * 查询病虫害识别记录
     *
     * @param id 病虫害识别记录主键
     * @return 病虫害识别记录
     */
    @Override
    public DiseaseIdentify selectPestRecognitionRecordById(Long id)
    {
        return pestRecognitionRecordMapper.selectPestRecognitionRecordById(id);
    }

    /**
     * 查询病虫害识别记录列表
     *
     * @param pestRecognitionRecord 病虫害识别记录
     * @return 病虫害识别记录
     */
    @Override
    public List<DiseaseIdentify> selectPestRecognitionRecordList(DiseaseIdentify pestRecognitionRecord)
    {
        return pestRecognitionRecordMapper.selectPestRecognitionRecordList(pestRecognitionRecord);
    }

    /**
     * 新增病虫害识别记录
     *
     * @param pestRecognitionRecord 病虫害识别记录
     * @return 结果
     */
    @Override
    public int insertPestRecognitionRecord(DiseaseIdentify pestRecognitionRecord)
    {
        pestRecognitionRecord.setCreateTime(new Date());
        pestRecognitionRecord.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return pestRecognitionRecordMapper.insertPestRecognitionRecord(pestRecognitionRecord);
    }

    /**
     * 修改病虫害识别记录
     *
     * @param pestRecognitionRecord 病虫害识别记录
     * @return 结果
     */
    @Override
    public int updatePestRecognitionRecord(DiseaseIdentify pestRecognitionRecord)
    {
        return pestRecognitionRecordMapper.updatePestRecognitionRecord(pestRecognitionRecord);
    }

    /**
     * 批量删除病虫害识别记录
     *
     * @param ids 需要删除的病虫害识别记录主键集合
     * @return 结果
     */
    @Override
    public int deletePestRecognitionRecordByIds(Long[] ids)
    {
        return pestRecognitionRecordMapper.deletePestRecognitionRecordByIds(ids);
    }

    /**
     * 删除病虫害识别记录信息
     *
     * @param id 病虫害识别记录主键
     * @return 结果
     */
    @Override
    public int deletePestRecognitionRecordById(Long id)
    {
        return pestRecognitionRecordMapper.deletePestRecognitionRecordById(id);
    }
}